<?php
namespace app\common\service;


class StudentsGoodsService extends BaseService
{

    public function getStudentsGoodsListByUid($sid, $uid, $p, $pageSize){
        $where = [
            'sid' => $sid,
            'uid' => $uid,
            'status' => 1
        ];
        $list = db('students_goods')
                ->field('goods_id,goods_name,goods_img,goods_price,is_price_discuss')
                ->where($where)
                ->order('edit_time DESC,add_time DESC')
                ->limit(($p - 1) * $pageSize, $pageSize)
                ->select();

        if($list){
            foreach($list as $k => $v){
                $list[$k]['goods_img'] = json_decode($v['goods_img'], 1);
            }
        }

        return $list;
    }

    /**
     * 微信端: 用户发布商品
     * @param $sid
     * @param $uid
     * @param $goods_id
     * @param $goods_info
     * @return int|string
     */
    public function saveStudentsGoods($sid, $uid, $goods_id = 0,$goods_info){
        $data = [
            'goods_name' => $goods_info['goods_name'],
            'goods_img' => json_encode($goods_info['goods_img']),
            'goods_price' => $goods_info['is_price_discuss'] ? 0.00 : $goods_info['goods_price'],
            'is_price_discuss' => $goods_info['is_price_discuss'],
        ];
        if($goods_id){
            $data['edit_time'] = time();
            $where = [
                'sid' => $sid,
                'goods_id' => $goods_id,
                'uid' => $uid,
                'status' => 1
            ];
            return db('students_goods')->where($where)->update($data);
        } else {
            $data['sid'] = $sid;
            $data['uid'] = $uid;
            $data['add_time'] = time();
            return db('students_goods')->insert($data);
        }
    }


    /**
     * 微信端: 用户获取商品详情
     * @param $sid
     * @param $uid
     * @param $goods_id
     * @return array
     */
    public function getStudentsGoodsDetail($sid, $uid, $goods_id){
        $where = [
            'sid' => $sid,
            'uid' => $uid,
            'goods_id' => $goods_id,
            'status' => 1
        ];

        $detail = db('students_goods')
                    ->field('goods_id,goods_name,goods_img,goods_price,is_price_discuss')
                    ->where($where)
                    ->find();
        if(!$detail){
            return ['status' => 0, 'msg' => '未找到商品'];
        }
        $detail['goods_img'] = json_decode($detail['goods_img'], 1);
        return ['status' => 1, 'data' => $detail, 'msg' => '获取成功'];
    }


    /**
     * 微信端: 删除商品
     * @param $sid
     * @param $uid
     * @param $goods_ids
     * @return int|string
     * @throws \think\Exception
     */
    public function deleteStudentsGoods($sid, $uid, $goods_ids){
        $where = [
            'sid' => $sid,
            'uid' => $uid,
            'goods_id' => ['in', $goods_ids],
            'status' => 1
        ];
        return db('students_goods')->where($where)->update(['edit_time' => time(), 'status' => -1]);
    }


    /**
     * 微信端: 客户上传公司产品图片
     * @param $sid
     * @param $uid
     * @param $media_id
     * @return array
     */
    public function uploadGoodsImg($sid, $uid, $media_id){
        $wxService = new WeixinService();
        //保存路径
        $save_path = ROOT_PATH . 'public' . DS . 'uploads'. DS . 'students_goods';
        if(!file_exists($save_path)){
            mkdir($save_path, 0777, true);
        }
        //文件命名
        $file_name = md5(date('YmdHis'));

        //调用下载接口并保存至相应地方
        $img_name = $wxService->download_temporary_media($sid, $media_id, $save_path, $file_name);

        //图片访问路径
        $img_url = $this->domain. DS .'uploads'. DS . 'students_goods'. DS . $img_name;

        $userResourcesService = new UserResourcesService();
        $user_resources_id = $userResourcesService->addUserResources($sid, $uid, $save_path, $img_name, $img_url);

        if(!$user_resources_id){
            return ['status' => 0, 'msg' => '上传失败'];
        }

        $r = $userResourcesService->getUserResourcesUrl($sid, $uid, $user_resources_id);
        return ['status' => 1, 'msg' => '上传成功', 'data' => $r];
    }
}







